<template>
  <div>
    <el-dialog
      :title="info.type==='add'?'新增菜单':'编辑菜单'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="ruleForm.type">
            <el-radio :disabled="info.type==='edit'" :label="1">目录</el-radio>
            <el-radio :disabled="info.type==='edit'" :label="2">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 选择菜单时才会出现下拉框 -->
        <el-form-item v-show="ruleForm.type === 2" label="上级" prop="pid">
          <el-select v-model="ruleForm.pid">
            <el-option label="顶级目录" :value="0"></el-option>
            <el-option v-for="(item, index) in menuList" :key="index" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type === 1" label="图标" prop="icon">
          <el-input v-model="ruleForm.icon"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type === 2" label="地址" prop="url">
          <el-input v-model="ruleForm.url"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button v-if="info.type==='add'" type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button>
          <el-button v-else type="success" @click="submitForm('ruleForm')"
            >立即修改</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {menuEdit} from '../../api'
export default {
  props: ["info"],
  data() {
    return {
      menuList: [],
      ruleForm: {
        // 上级id，parentid可能重复，id不会重复
        pid: 0,
        title: "",
        icon: "",
        // 一级目录，二级菜单
        type: 1,
        url: "",
        status: 1,
      },
      rules: {
        title: [
          { required: true, message: "请输入菜单标题", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.$http.get('/menulist?istree=1')
    .then(res => {
      this.menuList = res.list;
    })
  },
  methods: {
    // 给表单赋值
    setValue(info) {
      // 深拷贝，比避免数据互相影响
      this.ruleForm = {...info};
    },
    submitForm(formName) {
      this.$refs[formName].validate( async (valid) => {
        if (valid) {
          if(this.info.type==='add') {
            this.$http.post('/menuadd', this.ruleForm)
            .then(res => {
              console.log(res);
            })
            .catch(err => {
              console.log(err);
            })
          }else {
            // 编辑
            let res = await menuEdit(this.ruleForm);
          }
          // 关闭弹窗
          this.info.isShow = false;
          // 通知list再次获取数据  info->index->list
          this.$emit('update');
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
</style>